package com.abocode.aichat.app.configuration;


import com.iflytek.application.common.springjdbc.JdbcTemplateOperation;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by guanxf on 2015/12/16.
 */
@Configuration
public class DBConfiguration {

    @Value("${database.server.url}")
    private String databaseUrl;

    @Value("${database.server.driver}")
    private String databaseDriver;

    @Value("${database.server.username}")
    private String username;

    @Value("${database.server.password}")
    private String password;

    @Value("${database.server.maxActive}")
    private int maxActive;

    @Value("${database.server.maxIdle}")
    private int maxIdle;

    @Value("${database.server.removeAbandonedTimeout}")
    private int removeAbandonedTimeout;

    @Value("${database.server.maxWait}")
    private int maxWait;



    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl(databaseUrl);
        dataSource.setDriverClassName(databaseDriver);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(databaseUrl);
        dataSource.setMaxActive(maxActive);
        dataSource.setMinIdle(maxIdle);
        dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        return  dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        String  packages ="com.iflytek.app.user.model.*";
        String[] jpaPackageToScan = packages.split(",");
        Map<String, Object> jpaProperties = new HashMap<>();
        jpaProperties.put("javax.persistence.validation.mode", "none");
        jpaProperties.put("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider");
        jpaProperties.put("hibernate.archive.autodetection", "class");
        jpaProperties.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
        jpaProperties.put("show_sql", true);
        return builder.dataSource(dataSource()).properties(jpaProperties).packages(jpaPackageToScan).build();
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }

    @Bean
    public JdbcTemplateOperation JdbcTemplateOperation(){
        return  new JdbcTemplateOperation(new JdbcTemplate(dataSource()));
    }
    @Bean
    public JdbcTemplateOperation jdbcTemplateOperation(){
        return  new JdbcTemplateOperation(jdbcTemplate());
    }
}
